package users

import (
	"go_auth/ext/settings"
	"go_auth/utils/commons"
)

/**
权限信息  这里一旦是restful的话，要加上method,不然会有问题
*/

type Per struct {
	PerId  int    `xorm:" int(4)  pk  notnull unique autoincr 'per_id'"`
	Url    string `xorm:" varchar(200)    notnull   'url'"`
	Method string `xorm:" varchar(100)    notnull   'method'"`
	Desc   string `xorm:" varchar(100)    notnull   'desc'"`
	From   string `xorm:" varchar(100)    notnull   'from'"`
	Flag   int    `xorm:" int(4)    notnull  default 1  'flag'"`   // -1  1  url 监控
	Status int    `xorm:" int(4)    notnull  default 1  'status'"` // -1  1  删除
}

func GetAllPer(size int) interface{} {
	per := make([]Per, 0)
	find := settings.MasterDB.Limit(int(settings.AppBase.PageSize), size*int(settings.AppBase.PageSize)).Find(&per)
	if !commons.CheckErr(find) {
		return nil
	}
	return per
}

func (r *Per) UpdatePer() int64 {
	affected, err := settings.MasterDB.Id(r.PerId).Update(r)
	if commons.CheckErr(err) {
		return affected
	}
	return settings.Zero
}

func (r *Per) AddPer() int64 {
	insert, err := settings.MasterDB.Insert(r)
	if commons.CheckErr(err) {
		return insert
	}
	return settings.Zero
}

func (r *Per) DeletePer() int64 {
	affected, err := settings.MasterDB.Id(r.PerId).Delete(r)
	if commons.CheckErr(err) {
		return affected
	}
	return settings.Zero
}

func InitPer() ([]Per, error) {
	per := []Per{}
	find := settings.MasterDB.Find(&per)
	//if commons.CheckErr(find) {
	//	for _, v := range per {
	//		redis.SetValue(v.Url, v.PerId, 60*60*24*30*36, conf.RDb0)
	//		redis.SetValue(v.PerId, v.Url, 60*60*24*30*36, conf.RDb3)
	//	}
	//}

	return per, find

}
